/**
 * Created by Brook on 2020/4/9.
 */

(function () {
    var async = require('async');
    var moment = require('moment');
    var sql_config = require('../../serverconfig.js');
    var model_sql = require('../../tool/db_helper.js').init(sql_config.Model_Config);
    var SQL_TXT = sql_config.SQL_TXT;


    //接收请求 - 客户端信息 - 写入
    exports.post_clientinfo = function(req, res) {
        async.auto({
            check_pars:function (cb){
                res.header('Access-Control-Allow-Origin', '*');
                cb(null, req.body);
            },
            //回传数据
            response_data:['check_pars', function (cb, result) {
                var data = {
                    "message": "请求成功",
                    "data": {
                        "code": 200,
                        "data": []
                    }
                };
                console.log("Response request data success ...")
                res.json(data);
                cb(null, [])
            }],
            //删除数据
            del_data:['response_data', function (cb, result) {
                model_sql.query(SQL_TXT.del_clieninfo,[result.check_pars.appId],function(db_err,db_result){
                    db_err?cb(db_err):cb(null,db_result);
                });
            }],
            //记录数据
            insert_data:['del_data', function (cb, result) {
                var clientinfo = [];
                clientinfo = [
                    result.check_pars.appId,
                    result.check_pars.appPath,
                    result.check_pars.appVersion,
                    result.check_pars.isPackaged,
                    result.check_pars.localIp,
                    result.check_pars.port,
                    result.check_pars.public_ip,
                    result.check_pars.rvtToPbcExePath,
                    result.check_pars.saveMajorJsonFilePath,
                    result.check_pars.serverFileDownloadCover,
                    result.check_pars.serverOpened,
                    result.check_pars.serverSKPRVTFileSavePath,
                    result.check_pars.skpSupportVersion,
                    result.check_pars.skpToPbcExePath,
                    result.check_pars.hasQueue,
                    'true',
                    result.check_pars.platform,
                    result.check_pars.openedUI,
                    moment().format('YYYYMMDD'),
                    moment().format('YYYY-MM-DD HH:mm:ss')
                ];
                model_sql.query(SQL_TXT.insert_clieninfo,clientinfo,function(db_err,db_result){
                    console.log("insert sql success ... ")
                    db_err?cb(db_err):cb(null,db_result);
                });
            }]
        },function(err,results){
            if(err){
                console.log(err);
                res.json(r);
            }else{
                var r = {code:0,message:results.check_pars};
                //console.log(r);
            };
        });
    };


    //接收请求 - 客户端信息 - 查询
    exports.post_getclientinfo = function(req, res) {
        async.auto({
            check_pars:function (cb){
                res.header('Access-Control-Allow-Origin', '*');
                cb(null, req.body);
            },
            //查询数据
            check_info:['check_pars', function (cb, result) {
                //生成查询条件
                var generate = '';
                for(var i in result.check_pars) {
                    generate += i+" = " + "'" +result.check_pars[i]+ "' AND ";
                };
                //处理查询条件
                generate = generate.substring(0,generate.length-4);
                //查询数据
                model_sql.query(SQL_TXT.get_clieninfo + generate + " AND dataState='true'",function(db_err,db_result){
                    db_err?cb(db_err):cb(null,db_result);
                });
            }],
            //回传数据
            response_data:['check_info', function (cb, result) {
                var data = {
                    "message": "请求成功",
                    "data": {
                        "code": 200,
                        "data": result.check_info
                    }
                };
                console.log("Response request data success ...")
                res.json(data);
                cb(null, [])
            }],
        },function(err,results){
            if(err){
                console.log(err);
                res.json(r);
            }else{
                var r = {code:0,message:results.check_pars};
                //console.log(r);
            };
        });
    };

}).call(this);
